package com.jason.game.online.home.adapter

import android.content.Context
import com.cby.library.data.model.PosterBean
import android.widget.BaseAdapter
import com.jason.game.online.R
import android.view.ViewGroup
import android.view.LayoutInflater
import android.widget.TextView
import android.util.SparseArray
import android.view.View
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import java.util.ArrayList

/**
 * Created by xuzhiwei on 15/8/4.
 *
 * @author xuzhiwei
 */
class GameHomePosterAdapter(private val context: Context) : BaseAdapter() {
    private var mIndex = -1
    private var isVisible = View.GONE
    private val posterBeans: MutableList<PosterBean> = mutableListOf()

    fun setItemNewVisibility(convertView: View, visibility: Int) {
        val holder = convertView.tag as ViewHolder
        val iconImgNew = holder.findView<ImageView>(R.id.iv_gamelist_item_icon_new)!!
        iconImgNew.visibility = visibility
    }

    fun setItemViewVisibility(index: Int, visibility: Int) {
        mIndex = index
        isVisible = visibility
    }

    override fun getCount(): Int {
        return posterBeans.size
    }

    override fun getItem(position: Int): Any {
        return posterBeans[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View, parent: ViewGroup): View {
        var convertView = convertView
        val holder: ViewHolder
        if (null == convertView) {
            convertView =
                LayoutInflater.from(context).inflate(R.layout.layout_header_poster_item, null)
            holder = ViewHolder(convertView)
            convertView.tag = holder
        } else {
            holder = convertView.tag as ViewHolder
        }
        val bean = posterBeans[position]
        val iconImg = holder.findView<ImageView>(R.id.iv_game_list_item_icon)!!
        val gameNameTxt = holder.findView<TextView>(R.id.tv_game_list_item_name)!!
        val iconImgNew = holder.findView<ImageView>(R.id.iv_gamelist_item_icon_new)!!
        if (mIndex == position) {
            iconImgNew.visibility = isVisible
        } else {
            iconImgNew.visibility = View.GONE
        }
        val options = RequestOptions().apply {
            fitCenter()
            placeholder(R.drawable.loading_bixue)
            error(R.drawable.loading_bixue)
            diskCacheStrategy(DiskCacheStrategy.ALL)
        }
        Glide.with(context.applicationContext).load(bean.icon).apply(options).into(iconImg)
        gameNameTxt.text = bean.title
        return convertView
    }

    fun setDatas(data: List<PosterBean>?) {
        data?.let {
            posterBeans.clear()
            posterBeans.addAll(data)
        }
    }

    inner class ViewHolder(private val convertView: View) {
        private val views = SparseArray<View?>()
        fun <T : View> findView(resId: Int): T {
            var v = views[resId]
            if (null == v) {
                v = convertView.findViewById(resId)
                views.put(resId, v)
            }
            return v as T
        }

        fun setTextView(resId: Int, text: String?): TextView {
            val textView = findView<TextView>(resId)!!
            textView.text = text
            return textView
        }
    }
}